package com.sr.controller;

import com.sr.annotation.Log;
import com.sr.constant.Constant;
import com.sr.enums.ResponseCode;
import com.sr.pojo.SysUser;
import com.sr.service.IUserService;
import com.sr.util.R;
import com.sr.vo.PageVo;
import com.sr.vo.req.UserDetailReqVo;
import com.sr.vo.req.UserOwnRoleReqVo;
import com.sr.vo.req.UserPasswordReqVo;
import com.sr.vo.req.UserReqVo;
import com.sr.vo.resp.UserOwnRoleRespVo;
import com.sr.vo.resp.UserRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaochi
 * @date 2022/5/3 11:41
 * @desc UserController
 */
@Api(tags = "后台接口 -> 用户模块")
@RestController
@RequiredArgsConstructor(onConstructor = @__)
@RequestMapping(Constant.Api.BACK_PREFIX)
public class UserController extends BaseController{

    private final IUserService userService;
    private final BCryptPasswordEncoder passwordEncoder;

    @ApiOperation(value = "分页查询用户",notes = "分页查询用户接口")
    @Log(module = "组织管理-用户管理",action = "分页查询用户接口")
    @PreAuthorize("hasAuthority('sys:user:list')")
    @PostMapping("/users")
    public R<PageVo<SysUser>> pageInfo(@RequestBody UserReqVo reqVo){
        SysUser sysUser = SysUser.builder().deleted(1).build();
        return R.ok(userService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),sysUser));
    }

    @ApiOperation(value = "新增用户",notes = "新增用户")
    @PreAuthorize("hasAuthority('sys:user:add')")
    @Log(module = "组织管理-用户管理",action = "新增用户")
    @PostMapping("/user")
    public R<String> create(@RequestBody @Validated(UserReqVo.Create.class) UserReqVo reqVo){
        if (userService.getUserByUserName(reqVo.getUsername()) != null){
            return R.error(ResponseCode.ACCOUNT_EXISTS_ERROR.getMessage());
        }
        SysUser sysUser = reqVo.buildEntity();
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        return this.response(userService.insert(sysUser));
    }

    @Log(module = "组织管理-用户管理",action = "查询用户拥有的角色数据接口")
    @ApiOperation(value = "查询用户拥有的角色数据接口",notes = "查询用户拥有的角色数据接口")
    @GetMapping("/user/roles/{userId}")
    public R<UserOwnRoleRespVo> getUserOwnRole(@PathVariable("userId")String userId){
        return R.ok(userService.getUserOwnRole(userId));
    }

    @ApiOperation(value = "更新用户角色",notes = "保存用户拥有的角色信息接口")
    @PreAuthorize("hasAuthority('sys:user:role:update')")
    @Log(module = "组织管理-用户管理",action = "保存用户拥有的角色信息接口")
    @PutMapping("/user/roles")
    public R<String> saveUserOwnRole(@RequestBody @Valid UserOwnRoleReqVo reqVo){
        return this.response(userService.setUserOwnRole(reqVo));
    }

    @ApiOperation(value = "重新用户密码",notes = "重新用户密码接口")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "String",paramType = "form")
    @PreAuthorize("hasAuthority('sys:user:role:update')")
    @Log(module = "组织管理-重置用户密码",action = "重置用户密码接口")
    @GetMapping("/user/password/{id}")
    public R<String> resetUpdatePassword(@PathVariable("id") String userId){
        return this.response(userService.resetUpdatePassword(userId));
    }

    @ApiOperation(value = "更新用户",notes = "更新用户接口")
    @PreAuthorize("hasAuthority('sys:user:update')")
    @Log(module = "组织管理-用户管理",action = "列表修改用户信息接口")
    @PutMapping("/user")
    public R<String> updateUserInfo(@RequestBody @Validated(UserReqVo.Update.class) UserReqVo reqVo){
        return this.response(userService.updateUserInfo(reqVo,this.getUserId()));
    }

    @ApiOperation(value = "批量/删除用户",notes = "批量/删除用户接口")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @Log(module = "组织管理-用户管理",action = "批量/删除用户接口")
    @DeleteMapping("/user")
    public R<String> deletedUsers(@RequestBody @ApiParam(value = "用户id集合") List<String> list){
        return this.response(userService.deletedUsers(list,this.getUserId()));
    }

    @ApiOperation(value = "用户信息详情",notes = "用户信息详情接口")
    @Log(module = "组织管理-用户管理",action = "用户信息详情接口")
    @GetMapping("/user/info")
    public R<UserRespVo> detailInfo(){
        return R.ok(userService.detailInfo(this.getUserId()));
    }

    @ApiOperation(value = "保存个人信息",notes = "保存个人信息接口")
    @Log(module = "组织管理-用户管理",action = "保存个人信息接口")
    @PutMapping("/user/info")
    public R<String> saveUserInfo(@RequestBody UserDetailReqVo reqVo){
        return this.response(userService.userUpdateDetailInfo(reqVo.buildEntity(),this.getUserId()));
    }

    @ApiOperation(value = "修改个人密码",notes = "修改个人密码接口")
    @Log(module = "组织管理-用户管理",action = "修改个人密码接口")
    @PutMapping("/user/password")
    public R<String> updatePassword(@RequestBody @Valid UserPasswordReqVo userPasswordReqVo){
        return this.response(userService.userUpdatePassword(userPasswordReqVo,this.getUserId()));
    }
}
